<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */

load_theme_textdomain( 'kubrick' );

automatic_feed_links();

if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',
	));
}

/** @ignore */
function kubrick_head() {
	$head = "<style type='text/css'>\n<!--";
	$output = '';
	if ( kubrick_header_image() ) {
		$url =  kubrick_header_image_url() ;
		$output .= "#header { background: url('$url') no-repeat bottom center; }\n";
	}
	if ( false !== ( $color = kubrick_header_color() ) ) {
		$output .= "#headerimg h1 a, #headerimg h1 a:visited, #headerimg .description { color: $color; }\n";
	}
	if ( false !== ( $display = kubrick_header_display() ) ) {
		$output .= "#headerimg { display: $display }\n";
	}
	$foot = "--></style>\n";
	if ( '' != $output )
		echo $head . $output . $foot;
}

add_action('wp_head', 'kubrick_head');

function kubrick_header_image() {
	return apply_filters('kubrick_header_image', get_option('kubrick_header_image'));
}

function kubrick_upper_color() {
	if (strpos($url = kubrick_header_image_url(), 'header-img.php?') !== false) {
		parse_str(substr($url, strpos($url, '?') + 1), $q);
		return $q['upper'];
	} else
		return '69aee7';
}

function kubrick_lower_color() {
	if (strpos($url = kubrick_header_image_url(), 'header-img.php?') !== false) {
		parse_str(substr($url, strpos($url, '?') + 1), $q);
		return $q['lower'];
	} else
		return '4180b6';
}

function kubrick_header_image_url() {
	if ( $image = kubrick_header_image() )
		$url = get_template_directory_uri() . '/images/' . $image;
	else
		$url = get_template_directory_uri() . '/images/kubrickheader.jpg';

	return $url;
}

function kubrick_header_color() {
	return apply_filters('kubrick_header_color', get_option('kubrick_header_color'));
}

function kubrick_header_color_string() {
	$color = kubrick_header_color();
	if ( false === $color )
		return 'white';

	return $color;
}

function kubrick_header_display() {
	return apply_filters('kubrick_header_display', get_option('kubrick_header_display'));
}

function kubrick_header_display_string() {
	$display = kubrick_header_display();
	return $display ? $display : 'inline';
}

add_action('admin_menu', 'kubrick_add_theme_page');

function kubrick_add_theme_page() {
	if ( isset( $_GET['page'] ) && $_GET['page'] == basename(__FILE__) ) {
		if ( isset( $_REQUEST['action'] ) && 'save' == $_REQUEST['action'] ) {
			check_admin_referer('kubrick-header');
			if ( isset($_REQUEST['njform']) ) {
				if ( isset($_REQUEST['defaults']) ) {
					delete_option('kubrick_header_image');
					delete_option('kubrick_header_color');
					delete_option('kubrick_header_display');
				} else {
					if ( '' == $_REQUEST['njfontcolor'] )
						delete_option('kubrick_header_color');
					else {
						$fontcolor = preg_replace('/^.*(#[0-9a-fA-F]{6})?.*$/', '$1', $_REQUEST['njfontcolor']);
						update_option('kubrick_header_color', $fontcolor);
					}
					if ( preg_match('/[0-9A-F]{6}|[0-9A-F]{3}/i', $_REQUEST['njuppercolor'], $uc) && preg_match('/[0-9A-F]{6}|[0-9A-F]{3}/i', $_REQUEST['njlowercolor'], $lc) ) {
						$uc = ( strlen($uc[0]) == 3 ) ? $uc[0]{0}.$uc[0]{0}.$uc[0]{1}.$uc[0]{1}.$uc[0]{2}.$uc[0]{2} : $uc[0];
						$lc = ( strlen($lc[0]) == 3 ) ? $lc[0]{0}.$lc[0]{0}.$lc[0]{1}.$lc[0]{1}.$lc[0]{2}.$lc[0]{2} : $lc[0];
						update_option('kubrick_header_image', "header-img.php?upper=$uc&lower=$lc");
					}

					if ( isset($_REQUEST['toggledisplay']) ) {
						if ( false === get_option('kubrick_header_display') )
							update_option('kubrick_header_display', 'none');
						else
							delete_option('kubrick_header_display');
					}
				}
			} else {

				if ( isset($_REQUEST['headerimage']) ) {
					check_admin_referer('kubrick-header');
					if ( '' == $_REQUEST['headerimage'] )
						delete_option('kubrick_header_image');
					else {
						$headerimage = preg_replace('/^.*?(header-img.php\?upper=[0-9a-fA-F]{6}&lower=[0-9a-fA-F]{6})?.*$/', '$1', $_REQUEST['headerimage']);
						update_option('kubrick_header_image', $headerimage);
					}
				}

				if ( isset($_REQUEST['fontcolor']) ) {
					check_admin_referer('kubrick-header');
					if ( '' == $_REQUEST['fontcolor'] )
						delete_option('kubrick_header_color');
					else {
						$fontcolor = preg_replace('/^.*?(#[0-9a-fA-F]{6})?.*$/', '$1', $_REQUEST['fontcolor']);
						update_option('kubrick_header_color', $fontcolor);
					}
				}

				if ( isset($_REQUEST['fontdisplay']) ) {
					check_admin_referer('kubrick-header');
					if ( '' == $_REQUEST['fontdisplay'] || 'inline' == $_REQUEST['fontdisplay'] )
						delete_option('kubrick_header_display');
					else
						update_option('kubrick_header_display', 'none');
				}
			}
			//print_r($_REQUEST);
			wp_redirect("themes.php?page=functions.php&saved=true");
			die;
		}
		add_action('admin_head', 'kubrick_theme_page_head');
	}
	add_theme_page(__('Custom Header'), __('Custom Header'), 'edit_themes', basename(__FILE__), 'kubrick_theme_page');
}

function kubrick_theme_page_head() {
?>
<script type="text/javascript" src="../wp-includes/js/colorpicker.js"></script>
<script type='text/javascript'>
// <![CDATA[
	function pickColor(color) {
		ColorPicker_targetInput.value = color;
		kUpdate(ColorPicker_targetInput.id);
	}
	function PopupWindow_populate(contents) {
		contents += '<br /><p style="text-align:center;margin-top:0px;"><input type="button" class="button-secondary" value="<?php esc_attr_e('Close Color Picker'); ?>" onclick="cp.hidePopup(\'prettyplease\')"></input></p>';
		this.contents = contents;
		this.populated = false;
	}
	function PopupWindow_hidePopup(magicword) {
		if ( magicword != 'prettyplease' )
			return false;
		if (this.divName != null) {
			if (this.use_gebi) {
				document.getElementById(this.divName).style.visibility = "hidden";
			}
			else if (this.use_css) {
				document.all[this.divName].style.visibility = "hidden";
			}
			else if (this.use_layers) {
				document.layers[this.divName].visibility = "hidden";
			}
		}
		else {
			if (this.popupWindow && !this.popupWindow.closed) {
				this.popupWindow.close();
				this.popupWindow = null;
			}
		}
		return false;
	}
	function colorSelect(t,p) {
		if ( cp.p == p && document.getElementById(cp.divName).style.visibility != "hidden" )
			cp.hidePopup('prettyplease');
		else {
			cp.p = p;
			cp.select(t,p);
		}
	}
	function PopupWindow_setSize(width,height) {
		this.width = 162;
		this.height = 210;
	}

	var cp = new ColorPicker();
	function advUpdate(val, obj) {
		document.getElementById(obj).value = val;
		kUpdate(obj);
	}
	function kUpdate(oid) {
		if ( 'uppercolor' == oid || 'lowercolor' == oid ) {
			uc = document.getElementById('uppercolor').value.replace('#', '');
			lc = document.getElementById('lowercolor').value.replace('#', '');
			hi = document.getElementById('headerimage');
			hi.value = 'header-img.php?upper='+uc+'&lower='+lc;
			document.getElementById('header').style.background = 'url("<?php echo get_template_directory_uri(); ?>/images/'+hi.value+'") center no-repeat';
			document.getElementById('advuppercolor').value = '#'+uc;
			document.getElementById('advlowercolor').value = '#'+lc;
		}
		if ( 'fontcolor' == oid ) {
			document.getElementById('header').style.color = document.getElementById('fontcolor').value;
			document.getElementById('advfontcolor').value = document.getElementById('fontcolor').value;
		}
		if ( 'fontdisplay' == oid ) {
			document.getElementById('headerimg').style.display = document.getElementById('fontdisplay').value;
		}
	}
	function toggleDisplay() {
		td = document.getElementById('fontdisplay');
		td.value = ( td.value == 'none' ) ? 'inline' : 'none';
		kUpdate('fontdisplay');
	}
	function toggleAdvanced() {
		a = document.getElementById('jsAdvanced');
		if ( a.style.display == 'none' )
			a.style.display = 'block';
		else
			a.style.display = 'none';
	}
	function kDefaults() {
		document.getElementById('headerimage').value = '';
		document.getElementById('advuppercolor').value = document.getElementById('uppercolor').value = '#69aee7';
		document.getElementById('advlowercolor').value = document.getElementById('lowercolor').value = '#4180b6';
		document.getElementById('header').style.background = 'url("<?php echo get_template_directory_uri(); ?>/images/kubrickheader.jpg") center no-repeat';
		document.getElementById('header').style.color = '#FFFFFF';
		document.getElementById('advfontcolor').value = document.getElementById('fontcolor').value = '';
		document.getElementById('fontdisplay').value = 'inline';
		document.getElementById('headerimg').style.display = document.getElementById('fontdisplay').value;
	}
	function kRevert() {
		document.getElementById('headerimage').value = '<?php echo esc_js(kubrick_header_image()); ?>';
		document.getElementById('advuppercolor').value = document.getElementById('uppercolor').value = '#<?php echo esc_js(kubrick_upper_color()); ?>';
		document.getElementById('advlowercolor').value = document.getElementById('lowercolor').value = '#<?php echo esc_js(kubrick_lower_color()); ?>';
		document.getElementById('header').style.background = 'url("<?php echo esc_js(kubrick_header_image_url()); ?>") center no-repeat';
		document.getElementById('header').style.color = '';
		document.getElementById('advfontcolor').value = document.getElementById('fontcolor').value = '<?php echo esc_js(kubrick_header_color_string()); ?>';
		document.getElementById('fontdisplay').value = '<?php echo esc_js(kubrick_header_display_string()); ?>';
		document.getElementById('headerimg').style.display = document.getElementById('fontdisplay').value;
	}
	function kInit() {
		document.getElementById('jsForm').style.display = 'block';
		document.getElementById('nonJsForm').style.display = 'none';
	}
	addLoadEvent(kInit);
// ]]>
</script>
<style type='text/css'>
	#headwrap {
		text-align: center;
	}
	#kubrick-header {
		font-size: 80%;
	}
	#kubrick-header .hibrowser {
		width: 780px;
		height: 260px;
		overflow: scroll;
	}
	#kubrick-header #hitarget {
		display: none;
	}
	#kubrick-header #header h1 {
		font-family: 'Trebuchet MS', 'Lucida Grande', Verdana, Arial, Sans-Serif;
		font-weight: bold;
		font-size: 4em;
		text-align: center;
		padding-top: 70px;
		margin: 0;
	}

	#kubrick-header #header .description {
		font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
		font-size: 1.2em;
		text-align: center;
	}
	#kubrick-header #header {
		text-decoration: none;
		color: <?php echo kubrick_header_color_string(); ?>;
		padding: 0;
		margin: 0;
		height: 200px;
		text-align: center;
		background: url('<?php echo kubrick_header_image_url(); ?>') center no-repeat;
	}
	#kubrick-header #headerimg {
		margin: 0;
		height: 200px;
		width: 100%;
		display: <?php echo kubrick_header_display_string(); ?>;
	}
	#jsForm {
		display: none;
		text-align: center;
	}
	#jsForm input.submit, #jsForm input.button, #jsAdvanced input.button {
		padding: 0px;
		margin: 0px;
	}
	#advanced {
		text-align: center;
		width: 620px;
	}
	html>body #advanced {
		text-align: center;
		position: relative;
		left: 50%;
		margin-left: -380px;
	}
	#jsAdvanced {
		text-align: right;
	}
	#nonJsForm {
		position: relative;
		text-align: left;
		margin-left: -370px;
		left: 50%;
	}
	#nonJsForm label {
		padding-top: 6px;
		padding-right: 5px;
		float: left;
		width: 100px;
		text-align: right;
	}
	.defbutton {
		font-weight: bold;
	}
	.zerosize {
		width: 0px;
		height: 0px;
		overflow: hidden;
	}
	#colorPickerDiv a, #colorPickerDiv a:hover {
		padding: 1px;
		text-decoration: none;
		border-bottom: 0px;
	}
</style>
<?php
}

function kubrick_theme_page() {
	if ( isset( $_REQUEST['saved'] ) ) echo '<div id="message" class="updated fade"><p><strong>'.__('Options saved.').'</strong></p></div>';
?>
<div class='wrap'>
	<h2><?php _e('Customize Header'); ?></h2>
	<div id="kubrick-header">
		<div id="headwrap">
			<div id="header">
				<div id="headerimg">
					<h1><?php bloginfo('name'); ?></h1>
					<div class="description"><?php bloginfo('description'); ?></div>
				</div>
			</div>
		</div>
		<br />
		<div id="nonJsForm">
			<form method="post" action="">
				<?php wp_nonce_field('kubrick-header'); ?>
				<div class="zerosize"><input type="submit" name="defaultsubmit" value="<?php esc_attr_e('Save'); ?>" /></div>
					<label for="njfontcolor"><?php _e('Font Color:'); ?></label><input type="text" name="njfontcolor" id="njfontcolor" value="<?php echo esc_attr(kubrick_header_color()); ?>" /> <?php printf(__('Any CSS color (%s or %s or %s)'), '<code>red</code>', '<code>#FF0000</code>', '<code>rgb(255, 0, 0)</code>'); ?><br />
					<label for="njuppercolor"><?php _e('Upper Color:'); ?></label><input type="text" name="njuppercolor" id="njuppercolor" value="#<?php echo esc_attr(kubrick_upper_color()); ?>" /> <?php printf(__('HEX only (%s or %s)'), '<code>#FF0000</code>', '<code>#F00</code>'); ?><br />
				<label for="njlowercolor"><?php _e('Lower Color:'); ?></label><input type="text" name="njlowercolor" id="njlowercolor" value="#<?php echo esc_attr(kubrick_lower_color()); ?>" /> <?php printf(__('HEX only (%s or %s)'), '<code>#FF0000</code>', '<code>#F00</code>'); ?><br />
				<input type="hidden" name="hi" id="hi" value="<?php echo esc_attr(kubrick_header_image()); ?>" />
				<input type="submit" name="toggledisplay" id="toggledisplay" value="<?php esc_attr_e('Toggle Text'); ?>" />
				<input type="submit" name="defaults" value="<?php esc_attr_e('Use Defaults'); ?>" />
				<input type="submit" class="defbutton" name="submitform" value="&nbsp;&nbsp;<?php esc_attr_e('Save'); ?>&nbsp;&nbsp;" />
				<input type="hidden" name="action" value="save" />
				<input type="hidden" name="njform" value="true" />
			</form>
		</div>
		<div id="jsForm">
			<form style="display:inline;" method="post" name="hicolor" id="hicolor" action="<?php echo esc_attr($_SERVER['REQUEST_URI']); ?>">
				<?php wp_nonce_field('kubrick-header'); ?>
	<input type="button"  class="button-secondary" onclick="tgt=document.getElementById('fontcolor');colorSelect(tgt,'pick1');return false;" name="pick1" id="pick1" value="<?php esc_attr_e('Font Color'); ?>"></input>
		<input type="button" class="button-secondary" onclick="tgt=document.getElementById('uppercolor');colorSelect(tgt,'pick2');return false;" name="pick2" id="pick2" value="<?php esc_attr_e('Upper Color'); ?>"></input>
		<input type="button" class="button-secondary" onclick="tgt=document.getElementById('lowercolor');colorSelect(tgt,'pick3');return false;" name="pick3" id="pick3" value="<?php esc_attr_e('Lower Color'); ?>"></input>
				<input type="button" class="button-secondary" name="revert" value="<?php esc_attr_e('Revert'); ?>" onclick="kRevert()" />
				<input type="button" class="button-secondary" value="<?php esc_attr_e('Advanced'); ?>" onclick="toggleAdvanced()" />
				<input type="hidden" name="action" value="save" />
				<input type="hidden" name="fontdisplay" id="fontdisplay" value="<?php echo esc_attr(kubrick_header_display()); ?>" />
				<input type="hidden" name="fontcolor" id="fontcolor" value="<?php echo esc_attr(kubrick_header_color()); ?>" />
				<input type="hidden" name="uppercolor" id="uppercolor" value="<?php echo esc_attr(kubrick_upper_color()); ?>" />
				<input type="hidden" name="lowercolor" id="lowercolor" value="<?php echo esc_attr(kubrick_lower_color()); ?>" />
				<input type="hidden" name="headerimage" id="headerimage" value="<?php echo esc_attr(kubrick_header_image()); ?>" />
				<p class="submit"><input type="submit" name="submitform" class="button-primary" value="<?php esc_attr_e('Update Header'); ?>" onclick="cp.hidePopup('prettyplease')" /></p>
			</form>
			<div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;visibility:hidden;"> </div>
			<div id="advanced">
				<form id="jsAdvanced" style="display:none;" action="">
					<?php wp_nonce_field('kubrick-header'); ?>
					<label for="advfontcolor"><?php _e('Font Color (CSS):'); ?> </label><input type="text" id="advfontcolor" onchange="advUpdate(this.value, 'fontcolor')" value="<?php echo esc_attr(kubrick_header_color()); ?>" /><br />
					<label for="advuppercolor"><?php _e('Upper Color (HEX):');?> </label><input type="text" id="advuppercolor" onchange="advUpdate(this.value, 'uppercolor')" value="#<?php echo esc_attr(kubrick_upper_color()); ?>" /><br />
					<label for="advlowercolor"><?php _e('Lower Color (HEX):'); ?> </label><input type="text" id="advlowercolor" onchange="advUpdate(this.value, 'lowercolor')" value="#<?php echo esc_attr(kubrick_lower_color()); ?>" /><br />
					<input type="button" class="button-secondary" name="default" value="<?php esc_attr_e('Select Default Colors'); ?>" onclick="kDefaults()" /><br />
					<input type="button" class="button-secondary" onclick="toggleDisplay();return false;" name="pick" id="pick" value="<?php esc_attr_e('Toggle Text Display'); ?>"></input><br />
				</form>
			</div>
		</div>
	</div>
</div>
<?php }

/**
 * Jeccy @ Tue Nov 24 01:14:36 CST 2009
 * get authors'list
 * include_all默认是false，仅仅包含 user_level>2的用户
 */
function ms_list_authors($args = '') {
	global $wpdb;

	$defaults = array(
		'optioncount' => false, 'include_all'=>false,
		'show_name' => true,
		'show_avatar' => true, 'avatar_size' => 48, 'echo' => true,
		'style' => 'list'
	);
		
	$r = wp_parse_args( $args, $defaults );
	extract($r, EXTR_SKIP);
	$return = '';
	
	/** @todo Move select to get_authors(). */
	$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");
	
	$author_count = array();
	foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
		$author_count[$row->post_author] = $row->count;
	}
	$conti=0;
	foreach ( (array) $authors as $author ) {
		$author = get_userdata( $author->ID );
		//用户级别判断
		if ( empty($include_all) && $author->user_level<1) {
		 	//启用不包含全部,并且当前不是订阅用户
		 	continue;
		}
		//判定文章数是否为0
		$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
		if ( 'list' == $style ) $return.= '<li>';
		if ( ! empty($show_avatar)) {
			//输出头像
			$avatar='';
			$avatar= "<div class='avatar".$author->ID."'>";
			$avatar.='<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf("查看%s的所有文章", $author->display_name) ) . '">' . get_avatar($author->user_email,$avatar_size,'',$author->display_name) . '</a>';
			$avatar.= "</div>";
		}
		$return.=$avatar;
		if ($show_name) {
			$link = '<p>';
			$name = $author->display_name;
			if ( $posts == 0 ) {
				$link.= $name;
			} else {
				$link .= '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf("查看%s的所有文章", $author->display_name) ) . '">' . $name . '</a>';
			}
			$link.='</p>';
			if ( $optioncount ) { $link .= '<p class="gray">['. $posts . '篇]</p>'; }
		}
		$return.=$link;
		if ( 'list' == $style ) {
			$return .= '</li>';
		} else { $return .= $link . ', '; }
		
	}
	$return = trim($return, ', ');
	if ( !$echo ) {
		return $return;
	} else { echo $return; };
}
/**
 * @author wangwei 2010-4-8
 * 排行榜文章列表
 * most:评论最多的
 * new:最新发表的
 * $output list:列表li,array输出数组
 */
function walleve_posts_list($type='most',$limit=0,$output='list'){
	global $wpdb;
	$sql = "SELECT ID, post_title, comment_count FROM $wpdb->posts where post_type='post' ORDER BY comment_count DESC LIMIT $limit";
	$result = $wpdb->get_results($sql);
	if($output=='array'){
		return $result;
	} elseif ($output=='list') {
		$str = "";
		foreach ((array)$result as $key=>$value){
			$str.="<li class='posts_list_li'><a href='".get_permalink($value->ID)."' title='".$value->post_title."'>".$value->post_title."</li>";
		}
		echo $str;
	}
}
/**
 * @author wangwei 2010-4-10
 * $type==comment 评论最多的用户
 * $type==register 最新注册的用户
 */
function walleve_user_bang($type='comment'){
	global $wpdb;
	if($type=='comment'){
		$sql = "SELECT comment_ID,comment_author_url,comment_author,comment_author_email,user_id,COUNT(user_id) AS comment_comments FROM $wpdb->comments ";
		$sql .= "WHERE 1 AND comment_author NOT IN('mangse','walle') ";
		$sql .= "AND user_id != 0 ";
		$sql .= "AND comment_type != 'pingback' ";
		$sql .= "AND comment_approved = '1' AND comment_date > CURDATE() - INTERVAL 7 DAY ";	//一周内评论最多的人
		$sql .= "group by user_id ORDER BY comment_comments DESC LIMIT 36";
	}elseif($type=='register'){
		$sql = '';
	}
	$users = $wpdb->get_results($sql);
	if ($users){
		$str ='';
		foreach ((array)$users as $k=>$user){
			$str.="<li>";
			if ($user->user_id){
				$str.="<span class='imgwrap'><a href='".get_author_posts_url($user->user_id)."' target='_blank' title='".$user->comment_author."'>".get_avatar($user->comment_author_email,24)."</a></span>";
			}elseif($user->comment_author_url){
				$str.="<span class='imgwrap'><a href='".$user->comment_author_url."' target='_blank' title='".$user->comment_author."'>".get_avatar($user->comment_author_email,24)."</a></span>";
			}else{
				$str.="<span class='imgwrap'>".get_avatar($user->comment_author_email,24,'',"$user->comment_author")."</span>";
			}
			$str.="</li>";
		}
		echo $str;
	}else{
		echo "<li>".get_avatar('x@mangse.com',24)."</li>";
	}
}
/**
 * @author wangwei 2010-4-10
 * 判定用户是否登录
 */
function walleve_login($uid=''){
	global $user_ID,$current_user;
	if ($uid){
		$user_ID=$uid;
		$user = get_userdata($uid);
	}elseif (is_user_logged_in()){
		$user=$current_user;
	}
	if($user_ID){
		if ($user){
			$str = '<div id="sidebar_user"><span class="post_author">';
			$str .= '<div class="photo_left">'.get_avatar( $user->user_email, '48' ).'</div>';
			$str .= '<div class="author_right"><div class="author_right_top">';
			$str .='<span><a href="'.get_author_posts_url($user_ID) . '">'.$user->display_name.'</a></span>';
			$str .='</div><div class="author_right_bottom"><a href="'.get_author_posts_url($user_ID).'" title="查看文章">文章'.get_usernumposts($user_ID).'篇</a>';
			$str .= '&nbsp;|&nbsp;<a href="'.get_page_url('comment').'?m='.$user->user_login.'" title="查看评论">评论'.get_usernumcomments($user_ID).'条</a></div></div></span></div>';
			echo $str;
		}
	} else {
		/*
		$str = "<div id='sidebar_register'>";
		$str .= '<a href="' . get_page_url('login') . '" title="' . __('Log in') . '" class="login_link">' . __('Log in') . '</a>';
		if ( get_option('users_can_register') ){
			$str .= '&nbsp;&nbsp;<a href="' . get_page_url('register') . '" title="' . __('Register') . '" class="register_link">' . __('Register') . '</a>';
		}
		$str.= "</div>";
		echo $str;
		*/
	}
}
/**
 * @author wangwei 2010-4-11
 * 获取页面地址
 */
function get_page_url($post_name){
	global $wpdb;
	$page = $wpdb->get_row("select * from $wpdb->posts where post_name='$post_name' and post_status='publish' and post_type='page' limit 0,1");
	$link = get_permalink($page->ID);
	return $link;
}
function current_user(){
	global $current_user;
	return $current_user;
}
function get_usernumcomments($userid) {
	global $wpdb;
	$userid = (int) $userid;
	$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE user_id=$userid and comment_approved=1"));
	return $count;
}
function walleve_comment_list($user_id,$user_login,$page=1,$start=0,$limit=20,$showpage=3){
	global $wpdb;
	$linkurl = get_page_url('comment').'?m='.$user_login;
	$pages_to_show = 3;
	if( isset($page) ) {
		$page_now = intval($page);
	} 
	else {
		$page_now = 1;
	}
	$comnum = get_usernumcomments($user_id);
	$max_page = ceil($comnum/$limit);
	if( $page_now<1 || $max_page<$page_now) {
		$page_now = 1;
	}
	$offset = ($page_now-1) * $limit;
	$area = "DISTINCT ID,post_title,post_password,comment_ID,comment_author_email,comment_date,comment_post_ID,comment_author,comment_date_gmt,comment_approved,comment_type,comment_author_url,SUBSTRING(comment_content,1,52) AS com_excerpt";
	$sql = "SELECT $area FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID=$wpdb->posts.ID) WHERE user_id=$user_id and comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $offset, $limit";
	$comments = $wpdb->get_results($sql);
	$out_page = '<p style="text-align: center;">共有 '.$comnum.' 条';
	if( $max_page>1 ) {
		if( $page_now>1 ) {
			$out_page .= '<a href="'.$linkurl.'&page='.($page_now-1).'">&laquo;</a> ';
			}
		if( $page_now>($pages_to_show+1) ) {
			$out_page .= '<a href="'.$linkurl.'&page=1">1</a> ';
		}
		if( $page_now>($pages_to_show+2) ) {
			$out_page .= ' ... ';
		}
		for( $i=$page_now-$pages_to_show ; $i<=$page_now+$pages_to_show; $i++ ) {
			if( $i>=1 && $i<=$max_page ) {
				if($i == $page_now) {
					$out_page .= '[<strong>'.$i.'</strong>]';
				} 
				else {
					$out_page .= ' <a href="'.$linkurl.'&page='.$i.'">'.$i.'</a> ';
				}
			}
		}
		if( $page_now+$pages_to_show+1 < $max_page ) {
			$out_page .= ' ... ';
		}
		if( $page_now+$pages_to_show < $max_page ) {
			$out_page .= ' <a href="'.$linkurl.'&page='.$max_page.'">'.$max_page.'</a> ';
		}
		if( $page_now<$max_page ) {
			$out_page .= '<a href="'.$linkurl.'&page='.($page_now+1).'">&raquo;</a>';
		}
	}
	$out_page .= '</p>';
	echo "<ol id='page_commentlist'>";
	foreach ($comments as $comment) {//输出
		$output.="<li id=comment-".$comment->comment_ID.">";
		$output.="<span class='page_comment_h'>$comment->comment_date 发表于 <a href=" . get_permalink($comment->ID)."#comment-" . $comment->comment_ID." title='点击查看'>".$comment->post_title."</a></span>";
		$output.="<div class='comment_text'>".strip_tags($comment->com_excerpt)."</div>";
		$output.="</li>";
	}
	echo $output;
	echo "</ol>";
	echo($out_page);
}

function walleve_the_user_level($uid){
	$level = get_the_author_meta('user_level',$uid);
	if ($level>7){
		return "MSM";
	}elseif ($level>2){
		return "MSE";
	}elseif($level>1){
		return "MSC";	//作者
	}elseif ($level>0){
		return "MSB";	//投稿人
	}else{
		return "MSA";	//订阅者
	}
}

function walleve_get_category_parents( $id, $link = false, $before = '',$after='', $nicename = false, $visited = array() ) {
	$chain = '';
	$parent = &get_category( $id );
	if ( is_wp_error( $parent ) )
		return $parent;

	if ( $nicename )
		$name = $parent->slug;
	else
		$name = $parent->cat_name;

	if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
		$visited[] = $parent->parent;
		$chain .= walleve_get_category_parents( $parent->parent, $link, $before,$after, $nicename, $visited );
	}

	if ( $link )
		$chain .= $before.'<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->cat_name ) ) . '">'.$name.'</a>' . $after;
	else
		$chain .= $before.$name.$after;
	return $chain;
}
?>
